import asyncio
from actors.context_manager import CharacterContextManager


class StoryDirector:
    def __init__(self):
        # 定义角色名称与剧情阶段
        self.character_names = ["艾琳", "诺亚"]
        self.contexts = {}
        self.story_phases = ["相遇", "冲突", "理解"]
        self.phase_index = 0

    async def initialize_characters(self):
        print("【系统】剧本角色初始化中...\n")
        for name in self.character_names:
            self.contexts[name] = CharacterContextManager(name)
            self.contexts[name].initialize_context()
        print("【系统】初始化完成，当前参与角色：", "、".join(self.character_names))
        print("\n【背景】在遥远的未来，艾琳与诺亚在星舰上初次相遇，他们的命运即将交汇……")

    async def run_story_loop(self):
        print("\n【系统】剧情演化开始\n")
        for i in range(3):
            phase = self.story_phases[self.phase_index]
            print(f"\n—— 第{i + 1}轮 · 剧情阶段【{phase}】——")
            for name in self.character_names:
                ctx = self.contexts[name]
                prompt = ctx.build_prompt(phase)
                response = await ctx.generate_response(prompt)
                ctx.update_context(response)
                print(f"\n{name}：{response}")
            self.advance_phase()

        print("\n【系统】剧情推进结束")

    def advance_phase(self):
        if self.phase_index < len(self.story_phases) - 1:
            self.phase_index += 1
